require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var shopData=JSON.parse(sessionStorage.getItem("shopData"));
var freightData=JSON.parse(sessionStorage.getItem("freightData"+userInfo.chatId));
if(!userInfo){
    location.href="/loginIndex"
}
// var initData=JSON.parse(sessionStorage.getItem("data"));
// console.log(shopData)
var vm = new Vue({
    el:"#freightTemplate",
    data:{
        type:"",
        freightList:{},
        userFreightId:"0",
        productId:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        if(this.searchObject&&this.searchObject.userFreightId){
            this.userFreightId=this.searchObject&&this.searchObject.userFreightId;
        }
        if(this.searchObject&&this.searchObject.productId){
            this.productId=this.searchObject&&this.searchObject.productId;
        }
        this.getFreightList();
    },
    methods:{
        //后退
        getBack: function () {
            var path;
            path="/addGoods?type="+this.type;
            if(this.type==1&&this.productId){
                path+="&productId="+this.productId;
            }
            location.href=path;
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //运费列表
        getFreightList:function(){
            var that=this;
            var data={
                loginUid:userInfo.id
            };
            this.ajax({url:"/user/freightList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.freightList=b.json.data;
                    // that.goodslength=that.groupInfo.products.length;
                    // that.state=that.groupInfo.state;
                    // that.remind=that.groupInfo.remind;

                }else{
                    vm.$refs.prompt.toast("获取地址列表信息失败！")
                }
            });
        },
        //选中
        getSelect:function(item){
            var path;
            if(item){
                this.userFreightId=item.freightId;
                freightData.userFreightId=item.freightId;
                path="/addGoods?userFreightId="+item.freightId+"&freightName="+encodeURI(item.name)+"&type="+this.type;
            }else{
                freightData.userFreightId=0;
                path="/addGoods?userFreightId=0&freightName="+encodeURI("包邮")+"&type="+this.type;
            }

            sessionStorage.setItem("freightData"+userInfo.chatId,JSON.stringify(freightData));
            if(this.type==1&&this.productId){
                path+="&productId="+this.productId;
            }
            location.href=path;
            // console.log(freightId)
            //location.href="/addGoods?userFreightId="+item.freightId+"&freightName="+encodeURI(item.name);
        },
        getNext:function(type,item){
            if(type==1){
                location.href="/modifyTemplate?isSystem="+item.isSystem+"&freightId="+item.freightId;
            }else if(type==2){
                location.href="/newTemplate"
            }
        }
    },
    watch: {
        rCategorys:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('#screen .chooseCity-city')[0];

                var expansion = null; //是否存在展开的list
                var x, y, X, Y, swipeX, swipeY;

                container.addEventListener('touchstart', function(event) {
                    // console.log(container)
                    x = event.changedTouches[0].pageX;
                    y = event.changedTouches[0].pageY;
                    swipeX = true;
                    swipeY = true ;
                    // if(expansion){   //判断是否展开，如果展开则收起
                    //     expansion.className ="chooseCity-city clearfix";
                    // }
                    //event.stopPropagation();
                });
                container.addEventListener('touchmove', function(event){
                    event.stopPropagation();
                    X = event.changedTouches[0].pageX;
                    Y = event.changedTouches[0].pageY;
                    // 左右滑动
                    if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                        // 阻止事件冒泡
                        event.stopPropagation();
                        if(X - x > 10){   //右滑
                            // event.preventDefault();
                            this.className = "chooseCity-city clearfix";    //右滑收起
                        }
                        if(x - X > 10){   //左滑
                            event.preventDefault();
                            this.className = "chooseCity-city clearfix swipeleft";   //左滑展开
                            // expansion = this;
                        }
                        swipeY = false;
                    }
                    // 上下滑动
                    if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                        swipeX = false;
                    }
                });
            });
        }
        ,list:{
            handler:function(val, oldVal) {
                console.log(val)
            },
            deep: true
        }
    }
});